package com.weather.pm.beans;

import java.util.ArrayList;

import com.weather.pm.utils.DbHelper;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class City {
	public int id;
	public char firstLetter;
	public String name;
	public boolean isLove;

	public Weather weather;

	public static ArrayList<City> getCityList(Context context) {

		ArrayList<City> cities = new ArrayList<City>();
	
		
		
		SQLiteDatabase database = new DbHelper(context).getReadableDatabase();
		Cursor cursor = database.query("city", new String[] {"Id", "Name",
				"FirstLetter", "IsLove" }, null, null, null, null, null);
		while (cursor.moveToNext()) {
			City city = new City();
			city.id = cursor.getInt(0);
			city.name = cursor.getString(1);
			city.firstLetter = cursor.getString(2).charAt(0);
			int i = cursor.getInt(3);
			if (i != 0) {
				city.isLove = true;
			}
			cities.add(city);
		}
		cursor.close();
		database.close();
		return cities;

	}

	public static ArrayList<City> getMyCityList(Context context) {

		ArrayList<City> cities = new ArrayList<City>();
		SQLiteDatabase database = new DbHelper(context).getReadableDatabase();
		Cursor cursor = database.query("city", new String[] {"Id", "Name",
				"FirstLetter", "IsLove" }, " IsLove=1 ", null, null, null,
				null);
		while (cursor.moveToNext()) {
			City city = new City();
			city.id = cursor.getInt(0);
			city.name = cursor.getString(1);
			city.firstLetter = cursor.getString(2).charAt(0);
			int i = cursor.getInt(3);
			if (i != 0) {
				city.isLove = true;
			}
			cities.add(city);
		}
		cursor.close();
		database.close();
		return cities;

	}

	public static void insertCityList(Context context, ArrayList<City> citys) {
		SQLiteDatabase database = new DbHelper(context).getWritableDatabase();
		for (City city : citys) {
			ContentValues values = new ContentValues();
			values.put("Id", city.id);
			values.put("Name", city.name);
			values.put("FirstLetter", "" + city.firstLetter);
			
			database.insert("city", null, values);
			values = null;
		}
		database.close();
	}
   public static void setCityIsLove(Context context,int id){
	   SQLiteDatabase database = new DbHelper(context).getWritableDatabase();
	   ContentValues values = new ContentValues();
	   values.put("IsLove", 1);
	   database.update("city", values, "Id = "+id, null);
	   database.close();
	   values = null;
	   
   }
   public static void cancelCityIsLove(Context context,int id){
	   SQLiteDatabase database = new DbHelper(context).getWritableDatabase();
	   ContentValues values = new ContentValues();
	   values.put("IsLove", 0);
	   database.update("city", values, "Id = "+id, null);
	   database.close();
	   values = null;
	   
   }
   @Override
public boolean equals(Object o) {
	   City city =(City) o;
	// TODO Auto-generated method stub
	return this.id==city.id;
}
}
